/**
 * Copyright (C) 2009 Lazaro Brito
 *
 * This file is part of jIRC.
 * 
 * jIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * jIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with jIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.lazaro.jirc.test;

import org.lazaro.jirc.IRC;
import org.lazaro.jirc.irc.Channel;
import org.lazaro.jirc.irc.Target;
import org.lazaro.jirc.misc.Utils;
import org.lazaro.jirc.msg.TopicHandler;
import org.lazaro.jirc.msg.MessageHandler;
import org.lazaro.jirc.msg.KickHandler;

/**
 * @author Lazaro Brito
 *
 */
public class Main {
	public static void main(String[] args) {
		try {
			IRC irc = new IRC("lazllb.no-ip.biz", 6667);
			irc.getDetails().setPassword("12345");
			irc.getDetails().setNickName("NICK-jIRC2");
			irc.getDetails().setName("NAME-jIRC2");
			final Channel llbChannel = new Channel("#lazllb");
			irc.addMessageHandler(new MessageHandler() {
				@Override
				public void handle(IRC irc, Target from, Target location, String message) {
					if(message.startsWith("#")) {
						String cmd = message.substring(1);
						if(cmd.equals("ip")) {
							irc.getProtocol().sendMessage("My IP address is " + Utils.getExternalIP(), llbChannel);
						}
					}
					System.out.println(new StringBuilder().append(from).append(", ").append(location).append(", ").append(message).toString());
				}
			});
			irc.addTopicHandler(new TopicHandler() {
				@Override
				public void handle(IRC irc, Target from, Channel channel, String topic) {
					System.out.println(new StringBuilder().append(from).append(" changed the topic in channel ").append(channel).append(" to ").append(topic).append(".").toString());
				}
			});
			irc.addKickHandler(new KickHandler() {
				@Override
				public void handle(IRC irc, Target from, Channel channel, String message) {
					System.out.println(new StringBuilder().append(from).append(" kicked you from channel ").append(channel).append(" saying ").append(message).append(".").toString());
				}
			});
			irc.getProtocol().register();
			boolean done = false;
			while(irc.isConnected()) {
				irc.getProtocol().handle();
				if(!done && irc.isHasRegistered()) {
					irc.getProtocol().joinChannel(llbChannel);
					irc.getProtocol().sendMessage("hello world!", llbChannel);
					done = true;
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}